<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Core
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-06-01
 ***********************************************************************/
if(defined("IVANENGINE_CORE_IO_STORAGE"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_IO_STORAGE", true);

require_once(dirname(__FILE__).'/../Common.php');

abstract class CStorage extends CSingleton
{
    const   OT_AutoShutdown    = 'OT_AutoShutdown';
    
    protected   $m_options = array();
    
    public function __construct() 
    {
        parent::__construct();
    }
    
    public function __destruct() 
    {
        $this->Shutdown();
        unset($this->m_options);
        parent::__destruct();
    }
    
    /**
    * Initializes this Storage instance.
    *
    * Available options:
    *
    *  * auto_shutdown: Whether to automatically save the changes to the session (true by default)
    *
    * @param  array $options  An associative array of options
    *
    * @return bool true, if initialization completes successfully, otherwise false
    */
    public function Initialize($options = array())
    {
        $this->m_options = array_merge(array(
        self::OT_AutoShutdown => true,
        ), $options);
        
        if ($this->m_options[self::OT_AutoShutdown])
        {
            register_shutdown_function(array($this, 'Shutdown'));
        }
    }
    
    /**
    * Returns the option array.
    *
    * @return array The array of options
    */
    public function GetOptions()
    {
        return $this->m_options;
    }
    
    /**
    * Reads data from this storage.
    *
    * The preferred format for a key is directory style so naming conflicts can be avoided.
    *
    * @param  string $key  A unique key identifying your data
    *
    * @return mixed Data associated with the key
    */
    abstract public function Read($key);

    /**
    * Regenerates id that represents this storage.
    *
    * @param  boolean $destroy Destroy session when regenerating?
    *
    * @return boolean True if session regenerated, false if error
    */
    abstract public function Regenerate($destroy = false);

    /**
    * Removes data from this storage.
    *
    * The preferred format for a key is directory style so naming conflicts can be avoided.
    *
    * @param  string $key  A unique key identifying your data
    *
    * @return mixed Data associated with the key
    */
    abstract public function Remove($key);

    /**
    * Writes data to this storage.
    * The preferred format for a key is directory style so naming conflicts can be avoided.
    *
    * @param  string $key   A unique key identifying your data
    * @param  mixed  $data  Data associated with your key
    */
    abstract public function Write($key, $data);
    
    abstract public function Shutdown();
}
?>
